/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.application.service.impl;

import jakarta.annotation.Resource;
import org.gokeep.common.document.SystemConfig;
import org.gokeep.common.repository.SystemConfigRepository;
import org.gokeep.application.service.SystemConfigService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {
    @Resource
    private SystemConfigRepository systemConfigRepository;

    @Override
    public void initConfig(List<SystemConfig> systemConfigs) {
        systemConfigRepository.saveAll(systemConfigs);
    }

    @Override
    public void updateConfigByKey(String key, String value) {
        Optional<SystemConfig> systemConfigOptional = systemConfigRepository.findByKey(key);
        if (systemConfigOptional.isPresent()) {
            systemConfigOptional.get().setValue(value);
            systemConfigRepository.save(systemConfigOptional.get());
        }else {
            throw new RuntimeException("未发现相关配置");
        }
    }
}
